package com.fingard.dsp.bank.directbank.bos01;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BOS01SinglePay extends BOS01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail;
        String sendContent = null;
        BOS01Header tmpHeader = new BOS01Header();

        if ("190802".equals(p_req1908.reqHeader.commandCode)) {
            if (p_req1908.singleDetail.sameBnk.equals("1")) {
                if (p_req1908.singleDetail.isPrivate.equals("1")) {
                    sendContent = d_190802_SameBnkToPri(p_req1908, tmpHeader);
                } else {
                    sendContent = d_190802_SameBnkToPub(p_req1908, tmpHeader);
                }
            } else {
                if (p_req1908.singleDetail.sameCity.equals("1") && p_req1908.singleDetail.payAreaName.contains("上海")) {
                    sendContent = d_190802_DiffBnkToSHSameArea(p_req1908, tmpHeader);
                } else {
                    sendContent = d_190802_DiffBnkToDiffArea(p_req1908, tmpHeader);
                }
            }
        } else if ("190803".equals(p_req1908.reqHeader.commandCode)) {
            sendContent = d_190803_SmartPayment(p_req1908, tmpHeader);
        } else if ("190805".equals(p_req1908.reqHeader.commandCode)) {
            sendContent = d_190805_EBank(p_req1908, tmpHeader);
        }


        tmpRetDetail = d_190802_SglPay(p_req1908, sendContent, tmpHeader);
        tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    private String d_190805_EBank(ReqSingle p_req1908, BOS01Header p_bankHeader) throws Exception {
        p_bankHeader.opName = "transferSPBK_1Op";
        p_bankHeader.serialNo = p_req1908.singleDetail.askNumber;
        p_bankHeader.needSigned = true;
        p_bankHeader.writeParam = false;

        XmlTextWriter xmlWriter = new XmlTextWriter();
        writeCommonHeader(xmlWriter, p_bankHeader);
        XElement tmpXElement = new XElement("ReqParam", "");
        tmpXElement.addChild(new XElement("ACNO", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("ACNM", p_req1908.singleDetail.payName));
        tmpXElement.addChild(new XElement("OPAC", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("NAME", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("EXNO", p_req1908.singleDetail.cnapsCode));
        tmpXElement.addChild(new XElement("BNF", p_req1908.singleDetail.askNumber));
        tmpXElement.addChild(new XElement("COSE", ""));
        tmpXElement.addChild(new XElement("TRAM", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("USAG", p_req1908.singleDetail.purpose));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();
        return xmlWriter.toXmlString();
    }

    private String d_190802_SameBnkToPub(ReqSingle p_req1908, BOS01Header p_bankHeader) throws Exception {
        p_bankHeader.opName = "transferInner1_1Op";
        p_bankHeader.serialNo = p_req1908.singleDetail.askNumber;
        p_bankHeader.needSigned = true;
        p_bankHeader.writeParam = false;

        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, p_bankHeader);
        XElement tmpXElement = new XElement("ReqParam", "");
        tmpXElement.addChild(new XElement("ACNO", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("OPAC", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("TRAM", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("NAME", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("USAG", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("REMK", p_req1908.singleDetail.memo));
        tmpXElement.addChild(new XElement("COSE", ""));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();
        return xmlWriter.toXmlString();
    }

    private String d_190802_SameBnkToPri(ReqSingle p_req1908, BOS01Header p_bankHeader) throws Exception {
        p_bankHeader.opName = "transferPersonal1_1Op";
        p_bankHeader.serialNo = p_req1908.singleDetail.askNumber;
        p_bankHeader.needSigned = true;
        p_bankHeader.writeParam = false;

        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter, p_bankHeader);
        XElement tmpXElement = new XElement("ReqParam", "");
        tmpXElement.addChild(new XElement("ACNO", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("OPAC", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("TRAM", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("NAME", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("USAG", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("REMK", p_req1908.singleDetail.memo));
        tmpXElement.addChild(new XElement("COSE", ""));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();
        return xmlWriter.toXmlString();
    }

    private String d_190802_DiffBnkToSHSameArea(ReqSingle p_req1908, BOS01Header p_bankHeader) throws Exception {
        p_bankHeader.opName = "transferSHCity1_1Op";
        p_bankHeader.serialNo = p_req1908.singleDetail.askNumber;
        p_bankHeader.needSigned = true;
        p_bankHeader.writeParam = false;

        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, p_bankHeader);
        XElement tmpXElement = new XElement("ReqParam", "");
        tmpXElement.addChild(new XElement("ACNO", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("BCCD", p_req1908.singleDetail.cnapsCode));
        tmpXElement.addChild(new XElement("OPAC", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("NAME", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("TRAM", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("REMK", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("COSE", ""));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();
        return xmlWriter.toXmlString();
    }

    private String d_190802_DiffBnkToDiffArea(ReqSingle p_req1908, BOS01Header p_bankHeader) throws Exception {
        p_bankHeader.opName = "transferDiffCity1_1Op";
        p_bankHeader.serialNo = p_req1908.singleDetail.askNumber;
        p_bankHeader.needSigned = true;
        p_bankHeader.writeParam = false;

        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, p_bankHeader);
        XElement tmpXElement = new XElement("ReqParam", "");
        tmpXElement.addChild(new XElement("ACNO", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("OPAC", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("TRAM", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("EXNM", p_req1908.singleDetail.cnapsName));
        tmpXElement.addChild(new XElement("NAME", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("PBNO", p_req1908.singleDetail.cnapsCode));
        tmpXElement.addChild(new XElement("USAG", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("REMK", p_req1908.singleDetail.memo));
        tmpXElement.addChild(new XElement("COSE", ""));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();
        return xmlWriter.toXmlString();
    }

    private String d_190803_SmartPayment(ReqSingle p_req1908, BOS01Header p_bankHeader) throws Exception {
        p_bankHeader.opName = "transferCrossBank1_1Op";
        p_bankHeader.serialNo = p_req1908.singleDetail.askNumber;
        p_bankHeader.needSigned = true;
        p_bankHeader.writeParam = false;

        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter, p_bankHeader);
        XElement tmpXElement = new XElement("ReqParam", "");
        tmpXElement.addChild(new XElement("ACNO", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("VIRACNO", ""));
        tmpXElement.addChild(new XElement("OPAC", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("NAME", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("PBNO", getBankConfig().getMapValueByKey("PBNOMapping", p_req1908.singleDetail.recBankCode)));
        tmpXElement.addChild(new XElement("TRAM", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("USAG", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("REMK", p_req1908.singleDetail.memo));
        tmpXElement.addChild(new XElement("PATH", ""));
        tmpXElement.addChild(new XElement("PREDATE", ""));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();
        return xmlWriter.toXmlString();
    }

    private RetAtsSglDetail d_190802_SglPay(ReqSingle p_req1908, String p_sendContent, BOS01Header p_bankHeader) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();

        String[] tmpStrRet = sendToBank(p_sendContent, p_bankHeader);
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmprspcod = tmpBkReader.getFirstText("/BOSEBankData/opRep/retCode");
                String tmprspmsg = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");

                retDetail.respCode = "0";
                retDetail.respInfo = tmprspmsg;
                retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                retDetail.transState = tmprspcod;
                retDetail.payInfoCode = tmprspcod;
                retDetail.outTransID = tmpBkReader.getFirstText("/BOSEBankData/opRep/opResult/T24S");
                if (tmprspmsg.contains("非法SESSION") || tmprspmsg.contains("SESSION已超时")) {
                    bankFront.token = "";
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }


    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        BOS01Header bankHeader = new BOS01Header();
        bankHeader.opName = "queryTransferResult1_2Op";
        if ("190803".equals(p_req1944.reqHeader.commandCode)) {
            bankHeader.opName = "queryTransferResult2_1Op";
        }
        bankHeader.serialNo = getQryTransID();
        bankHeader.needSigned = false;

        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeElementString("OSNO", p_req1944.askNumber);
        xmlWriter.writeEndDocument();

        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmpRetCode = tmpBkReader.getFirstText("/BOSEBankData/opRep/retCode");
                String tmpRetInfo = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");
                if (tmpRetCode != null && tmpRetCode.equals("0")) {
                    if (tmpBkReader.read("/BOSEBankData/opRep/opResult")) {
                        retDetail.respCode = "0";
                        retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("REMG");
                        retDetail.reqSeqID = p_req1944.askNumber;
                        retDetail.transState = tmpRetCode + "+" + tmpBkReader.readedXElement.getSingleText("RECO");
                        retDetail.payInfoCode = tmpRetCode + "+" + tmpBkReader.readedXElement.getSingleText("RECO");
                        if ("190803".equals(p_req1944.reqHeader.commandCode)) {
                            retDetail.payInfoCode = tmpBkReader.readedXElement.getSingleText("REMG");
                        }
                        retDetail.transAmt = tmpBkReader.readedXElement.getSingleText("Amt");
                        retDetail.outTransID = tmpBkReader.readedXElement.getSingleText("T24S");
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpRetInfo;
                }
                if (tmpRetInfo.contains("非法SESSION") || tmpRetInfo.contains("SESSION已超时")) {
                    bankFront.token = "";
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1944.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
